package org.noote.test_camera_bc_client;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import org.noote.libs.camera.broadcast.CameraClient;
import org.noote.libs.graphicbuffer.GraphicBuffer;

public class TestCameraClient_Activity extends Activity {
	private CameraClient mClient = null;
	
	private String m_sServerIPAddress = "192.168.0.0";
	private int m_iServerPort = 8088;
	
	protected static final int MESSAGE_UI_CONNECTED=0;
	protected static final int MESSAGE_UI_DISCONNECTED=1;
	protected static final int MESSAGE_UI_GETERROR=2;

	public static final String PREFS_NAME = "BC_Client";
	public static final String PREFS_NAME_LATEST_IPADDRESS = "latest_ipaddress";
	
	final Handler _UI_Handler = new Handler() {
	    @Override
		public void handleMessage(Message msg) {
	        switch (msg.what) {
	        case MESSAGE_UI_CONNECTED:
	        {
	        	// start low mjpeg ^^
				mClient.askJpegPreview(30);
				Toast.makeText(getApplicationContext(), "Connected !", Toast.LENGTH_SHORT).show();
	        }
	            break;

	        case MESSAGE_UI_DISCONNECTED:
	        {
	        	Toast.makeText(getApplicationContext(), "Disconnected !", Toast.LENGTH_SHORT).show();
	        	
				EditText editServerIP = (EditText) findViewById(R.id.editServerIP);
				editServerIP.setEnabled(true);
				EditText editServerPort = (EditText) findViewById(R.id.editServerPort);
				editServerPort.setEnabled(true);
	        }
	            break;

	        case MESSAGE_UI_GETERROR:
	        {
	        	Toast.makeText(getApplicationContext(), "Get Error : "+(String) msg.obj, Toast.LENGTH_LONG).show();
	        }
	        	break;
   	        }
	    }
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
        SharedPreferences settings = getSharedPreferences(TestCameraClient_Activity.PREFS_NAME, 0);
        m_sServerIPAddress = settings.getString(TestCameraClient_Activity.PREFS_NAME_LATEST_IPADDRESS, m_sServerIPAddress);

		mClient = new CameraClient(this);
		((FrameLayout) findViewById(R.id.layoutPreview)).addView(mClient);

		mClient.setOnEventListener(new CameraClient.onEventListener()
		{
			@Override
			public void onConnected()
			{
				_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_CONNECTED, null));
			}
			@Override
			public void onDisconnected()
			{
				_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_DISCONNECTED, null));
			}
			@Override
			public void onError(String sMessage)
			{
				_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_GETERROR, sMessage));
			}
			@Override
			public void onGrabbedFrame(GraphicBuffer frame)
			{
				// ok get a frame and ask for a new one !
				mClient.askJpegPreview(30);
			}
		});

		
		EditText editServerIP = (EditText) findViewById(R.id.editServerIP);
		editServerIP.setText(m_sServerIPAddress);
		
		EditText editServerPort = (EditText) findViewById(R.id.editServerPort);
		editServerPort.setText(Integer.toString(m_iServerPort));
		
		Button buttonConnect = (Button) findViewById(R.id.buttonConnect);
		buttonConnect.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				EditText editServerIP = (EditText) findViewById(R.id.editServerIP);
				m_sServerIPAddress = editServerIP.getText().toString();
				
				EditText editServerPort = (EditText) findViewById(R.id.editServerPort);
				m_iServerPort = Integer.parseInt(editServerPort.getText().toString());
				
				editServerIP.setEnabled(false);
				editServerPort.setEnabled(false);
				
				Button buttonConnect = (Button) findViewById(R.id.buttonConnect);
				buttonConnect.setVisibility(View.GONE);
				
				Button buttonDisconnect = (Button) findViewById(R.id.buttonDisconnect);
				buttonDisconnect.setVisibility(View.VISIBLE);

		        SharedPreferences settings = getSharedPreferences(TestCameraClient_Activity.PREFS_NAME, 0);
		        SharedPreferences.Editor settings_editor = settings.edit(); 
		        settings_editor.putString(TestCameraClient_Activity.PREFS_NAME_LATEST_IPADDRESS, m_sServerIPAddress);
		        settings_editor.commit();
		        
				mClient.connect(m_sServerIPAddress, m_iServerPort);       	
			}
        });
		
		Button buttonDisconnect = (Button) findViewById(R.id.buttonDisconnect);
		buttonDisconnect.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				EditText editServerIP = (EditText) findViewById(R.id.editServerIP);
				editServerIP.setEnabled(true);
				
				EditText editServerPort = (EditText) findViewById(R.id.editServerPort);
				editServerPort.setEnabled(false);				

				Button buttonConnect = (Button) findViewById(R.id.buttonConnect);
				buttonConnect.setVisibility(View.VISIBLE);
				
				Button buttonDisconnect = (Button) findViewById(R.id.buttonDisconnect);
				buttonDisconnect.setVisibility(View.GONE);
				
				mClient.disconnect();
			}
        });
	}
}
